package cn.hnh.config;

import cn.hnh.constrant.ResultInfo;
import cn.hnh.service.RouteService;
import cn.hnh.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author HuangNianHong
 * @Time 2020/12/12 17:31
 */
@Component("AdminRouteInterceptors")
@Slf4j
public class AdminRouteInterceptors implements HandlerInterceptor {

    @Autowired
    @Lazy
    private RouteService routeService;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入拦截器");
        Result result = routeService.CheckIsLogin();
        if(!result.getCode().equals(ResultInfo.SUCCESS_CODE)){
            log.info("请求url地址:"+request.getRequestURL());
            log.info("未获得管理员权限");
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }
}
